package com.itheima.exception;


import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(
            HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse,
            Object handler,
            Exception e) {
        System.out.println("my exception is running ..."+e);
        ModelAndView mv = new ModelAndView();
        if(e instanceof NullPointerException){
            mv.addObject("msg","空指针异常");
        }else if(e instanceof ArithmeticException){
            mv.addObject("msg","算数运算异常");
        }else {
            mv.addObject("msg","未知的异常");
        }

        mv.setViewName("error.jsp");
        return mv;
    }
}
